package com.fitc;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Properties;
import java.util.Set;
import com.fitc.json.DatabaseHelper;
import com.fitc.json.Photo;
import com.fitc.json.Reader;

/**
 * Downloads jsons from Flickr api and stores image information in MySQL
 * database.
 * 
 * @author Jon
 * 
 */
public class PhotoInfoProgram {

	private static final boolean DATABASE_OFF = false;

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		Properties prop = new Properties();

		// load a properties file
		prop.load(new FileInputStream("config.properties"));

		Reader.setDayToStart(prop.getProperty("next_date"));
		Reader.setTotalNumberOfDays(Integer.parseInt(prop
				.getProperty("number_of_days")));
		Reader.setDaysAtATime(Integer.parseInt(prop
				.getProperty("days_at_a_time")));

		DatabaseHelper db;
		if (!DATABASE_OFF)
			db = new DatabaseHelper();
		boolean done = false;

		while (!done) {
			Reader r = new Reader();
			done = r.execute();
			Set<Photo> photos = r.getPhotos();
			if (!DATABASE_OFF)
				db.insertPhotoSet(photos);
			
			//store last successful parse into config file
			prop.setProperty("next_date", r.getNextDate());
    		//save properties to project root folder
    		prop.store(new FileOutputStream("config.properties"), null);
		}

		// Closes database
		if (!DATABASE_OFF)
			if (db != null){
				db.close();
				System.out.println("Database closed. Job done.");
			}	
	}

}
